//
//  AddTypeViewController.m
//  jiyujiaProgram
//
//  Created by Jame on 15/10/27.
//  Copyright © 2015年 中良 赵. All rights reserved.
//

#import "AddTypeViewController.h"
#import "AddTypeCell.h"
#import "DictModel.h"

@interface AddTypeViewController () <UITableViewDataSource,UITableViewDelegate>


@end

@implementation AddTypeViewController
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSIndexPath *_tempIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"上课方式"];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button.png")];
    [self creatNavigationBarRightItemWithRightTitle:@"确定" RightImage:nil];
    
    [self creatTableView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self creatHttpRequest];
}

- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)rightBtnClick:(id)rightSender
{
     DictData *data = _dataArray[_tempIndex.section];
    [self.navigationController popViewControllerAnimated:YES];
    if (self.addType) {
        self.addType(data.name);
    }
}


- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"JB":@"2",
                            @"PARENT_ID":@"d33a953a8da340639acc51c1123e7e28"
                            };
    
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(FINDDIC) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [DictModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"DictData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            DictModel *dict = [[DictModel alloc] init];
            dict = [DictModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:dict.data];
            [_tableView reloadData];
        }
    }];
}

- (void)creatTableView
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 64, ECSCREEN_W - 20, ECSCREEN_H - 64) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.backgroundColor = BG_COLOR;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 10)];
    view.backgroundColor = BG_COLOR;
    return view;
}


#pragma mark - tableViewHeightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.f;
}


#pragma mark - tableViewCellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"addTypeCell";
    AddTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"AddTypeCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[AddTypeCell class]]) {
                cell = (AddTypeCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    cell.clipsToBounds = YES;
    cell.layer.cornerRadius = 5;
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != _tempIndex.section) {
        AddTypeCell *cell = [_tableView cellForRowAtIndexPath:_tempIndex];
        cell.contentView.backgroundColor = ECCOLOR(255, 255, 255, 1);
        cell.titleLabel.textColor = ECCOLOR(0, 0, 0, 1);
    }
    AddTypeCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = NAVC_COLOR;
    cell.titleLabel.textColor = ECCOLOR(255, 255, 255, 1);
    _tempIndex = indexPath;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
